local tab_quality = Config.IconQuality
local tab_frame = Config.IconFrame

---@class HeroIcon
HeroIcon = Class("heroIcon")
local t = HeroIcon
function t:ctor()
    self.enableClick = true
    self.isSelect = false
end
---@param obj GComponent
function t:Bind(obj)
    self.bind = {
        obj = obj,
        qualityBG = obj:GetChild("qualityBg"),
        quality = obj:GetChild("quality"),
        icon = obj:GetChild("icon"),
        ---@type GList
        starList = obj:GetChild("star"),
        level = obj:GetChild("level"),
        ctrlSelect = obj:GetController("ctrlSelect")
    }
    self.bind.obj.onClick:Clear()
    self.bind.obj.onClick:Add(self.ClickItem,self)

    self.bind.ctrlSelect.selectedIndex = 0
end
function t:EnableClick(enable)
    self.enableClick = enable
end
function t:Refresh(data,clickCallback)
    self.data = data
    self.clickCallback = clickCallback
    self.bind.level.text = ""
    if data.icon ~= nil then
        self.bind.icon.url = EnumConst.AtlasPath.ICONAtlas..data.icon
    end
    if data.quality then
        local q = data.quality
        if q == nil or q ==0 then
            q =1;
        elseif q > #tab_quality then
            q = #tab_quality;
        end
        self.bind.quality.url = EnumConst.AtlasPath.ICONAtlas..tab_frame[q]
        self.bind.qualityBG.url = EnumConst.AtlasPath.ICONAtlas..tab_quality[data["quality"]]
    end
    if data.level then
        self.bind.level.text = string.format(getLanguage("Level"), data.level);
    end
    if data.star then
        self.bind.starList.visible = true
        local numstar = tonumber(data.star);
        if numstar > 5 then
            numstar = 5;
        end
        self.bind.starList:RemoveChildrenToPool()
        for i = 1, numstar do
            local starItem = self.bind.starList:AddItemFromPool()
            local ctrl = starItem:GetController("cstar")
            if ctrl then
                ctrl.selectedIndex = 1
            end
        end
    else
        self.bind.starList.visible = false
    end
end
function t:SetSelect(isSelect)
    self.isSelect = isSelect
    self.bind.ctrlSelect.selectedIndex = isSelect and 1 or 0
end
function t:ClickItem()
    if not self.enableClick then
        return
    end
    if self.clickCallback then
        self.clickCallback(self)
    end
end

function t:OnDestroy()
    if self.bind and self.bind.obj then
        self.bind.obj.onClick:Clear()
    end
    table.clear(self.bind)
    self.bind = nil

end

return t